<template>
    <div>
        <!-- //顶部栏 -->
       <van-nav-bar
  title="登录"
  left-text="返回"
  left-arrow
  @click-left="$router.go(-1)"
/>
<!-- 表单 -->

<van-form @submit="onSubmit">
 <van-field 
 v-model="mobile" 
 name="mobile" 
 required label="手机号"
  placeholder="请输入手机号"
   :rules="[
          { required: true, message: '请填写手机号' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式不对'
          }
        ]"
  /> 

<van-field 
v-model="code"
 name="code" 
 required 
 label="验证码"
  placeholder="请输入验证码"
  :rules="[
          { required: true, message: '验证码没有写哦!' },
          { pattern: /^\d{6}$/, message: '验证码格式不对' }
        ]"
   /> 

    <div style="margin: 16px;">
    <van-button 
    round block 
    type="info"
    native-type="submit"
   :disabled="isLoading"  
   :loading="isLoading" 
   loading-type="spinner"
     loading-text="登录中..."
     >提交</van-button>
  </div>
</van-form>

    </div>
</template>
0
<script>
//导入接口
import { getLogin } from "./user.js";
//导入token.js
import { saveToken } from "@/utils/token.js";
export default {
  name: "login",

  data() {
    return {
      mobile: "",
      code: "",
      isLoading: false //登录按钮禁用的状态
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      //1.设置登录按钮禁用状态
      this.isLoading = true;

      //2.调用登录接口
      getLogin(values)
        .then(res => {
          console.log(res);
          //1.弹窗提示
          this.$toast.success("登录成功");
          saveToken(res.data.data);
          //跳转首页
          this.$router.push("/");
        })
        .catch(err => {
          // 弹窗提示
          this.$toast.fail("账号或密码错误");
        })
        .then(() => {
          //请求完成后取消弹窗
          setTimeout(() => {
            this.isLoading = false;
          }, 1000);
        });
    }
  }
};
</script>

<style lang="less">
.van-nav-bar__content {
  background-color: #0098fa;
  .van-nav-bar__left {
    .van-nav-bar__text {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  .van-nav-bar__title {
    color: #fff;
  }
}
</style>
